package com.kkd.kkdapp.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.android_async_http.AsyncHttpResponseHandler;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.AppUtil.onToastDismiss;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

/**
 * 修改登录密码
 * 
 * @author mty
 * 
 */
public class UpdateLoginActivity extends BaseActivity implements
		OnClickListener {

	private RelativeLayout updatelogin_img; // 返回
	private EditText former_loginpw; // 就登录密码
	private EditText new_loginpw; // 新密码
	private EditText affirm_loginpw; // 确认密码
	private Button uplogin_bt; // 下一步
	// private String version; // 当前App版本号
	private boolean flag;
	private boolean SUBMITFLAG = true;
	private Handler mHandler = new Handler();
	private Dialog loadDialog;
	private RelativeLayout forgot_password; // 忘记密码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_login);
		initView();
		setListener();
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		updatelogin_img = (RelativeLayout) this
				.findViewById(R.id.updatelogin_img);
		former_loginpw = (EditText) this.findViewById(R.id.former_loginpw);
		new_loginpw = (EditText) this.findViewById(R.id.new_loginpw);
		affirm_loginpw = (EditText) this.findViewById(R.id.affirm_loginpw);
		uplogin_bt = (Button) this.findViewById(R.id.uplogin_bt);
		forgot_password = (RelativeLayout) this
				.findViewById(R.id.forgot_password);

	}

	/**
	 * 注册监听
	 */
	public void setListener() {
		updatelogin_img.setOnClickListener(this);
		uplogin_bt.setOnClickListener(this);
		forgot_password.setOnClickListener(this);
		former_loginpw.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (!"".equals(former_loginpw.getText().toString())
						&& !"".equals(new_loginpw.getText().toString())
						&& !"".equals(affirm_loginpw.getText().toString())) {
					uplogin_bt.setEnabled(true);
					flag = true;
				} else {
					uplogin_bt.setEnabled(false);
					flag = false;
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		new_loginpw.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (!"".equals(former_loginpw.getText().toString())
						&& !"".equals(new_loginpw.getText().toString())
						&& !"".equals(affirm_loginpw.getText().toString())) {
					uplogin_bt.setEnabled(true);
					flag = true;
				} else {
					uplogin_bt.setEnabled(false);
					flag = false;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		affirm_loginpw.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (!"".equals(former_loginpw.getText().toString())
						&& !"".equals(new_loginpw.getText().toString())
						&& !"".equals(affirm_loginpw.getText().toString())) {
					uplogin_bt.setEnabled(true);
					flag = true;
				} else {
					uplogin_bt.setEnabled(false);
					flag = false;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.updatelogin_img:
			finish();
			break;
		case R.id.uplogin_bt:
			if (SUBMITFLAG) {
				SUBMITFLAG = false;
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						SUBMITFLAG = true;
					}
				}, 2000);
				if (flag) {
					if (new_loginpw.getText().toString()
							.equals(affirm_loginpw.getText().toString())) {
						if (new_loginpw.length() < 6) {
							showToast(R.string.password_lenght);
						} else {
							if (userstringFilter(new_loginpw.getText()
									.toString())) {
								updatePassWord();
							} else {
								showToast(R.string.new_password);
							}
						}
					} else {
						showToast(R.string.registerActivity_button);
					}
				}
			}
			break;
		case R.id.forgot_password:
			Intent intent = new Intent(this, ResetPswStepActivity.class);
			intent.putExtra("fixed", true);
			startActivity(intent);
			break;

		}

	}

	/**
	 * 修改登录密码接口
	 */
	public void updatePassWord() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(this, "努力加载中...");
		KkdHttpMagager.getInstance().getUpdateLoginActivity(
				former_loginpw.getText().toString(),
				new_loginpw.getText().toString(),
				affirm_loginpw.getText().toString(), String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object res) {
						try {
							JSONObject request = new JSONObject(res.toString());
							if (request.getInt("code") == 0) {

								AppUtil.makeToastForCustomer(
										UpdateLoginActivity.this,
										R.string.update_ok,
										new onToastDismiss() {

											@Override
											public void onToastDismiss() {
												finish();
											}
										});
							} else {
								showToast(request.getString("desc"));
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						if (loadDialog != null) {
							loadDialog.dismiss();
						}
					}

					@Override
					public void onFailure(int statusCode, String responseBody) {
						showToast(responseBody);
						if (loadDialog != null) {
							loadDialog.dismiss();
						}
					}
				});
		// KkdHttpMagager.getInstance().getUpdateLoginActivity(cat[1],
		// former_loginpw
		// .getText().toString(), new_loginpw.getText().toString(),
		// affirm_loginpw.getText().toString(), SystemUtil.getVersionName(this),
		// cat[0],
		// String.class,new HttpCallBack() {
		//
		// @Override
		// public void onSuccess(int statusCode, Object responseBody) {
		// try {
		// String responseString = responseBody.toString();
		// JSONObject request = new JSONObject(responseString);
		// Toasttool.MyToast(UpdateLoginActivity.this,
		// R.string.update_ok);
		// finish();
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }
		// AppUtil.stopProgressDialog();
		// }
		//
		// @Override
		// public void onFailure(int statusCode, String desc) {
		// Toasttool.MyToast(UpdateLoginActivity.this,desc);
		// AppUtil.stopProgressDialog();
		// }
		// });
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
	}
}
